import 'dart:ui';

import '../utils/common_utils.dart';
import '../utils/constants_utils.dart';

/// 调色盘
class PaletteModel {
  int? id;

  ///  版本号
  String version;

  /// 调色板类型
  String name;

  /// 最后使用的颜色
  int lastUseColor;

  /// 色值
  List<ColorItem> data;

  PaletteModel({
    this.id,
    required this.version,
    required this.name,
    required this.lastUseColor,
    required this.data,
  });

  PaletteModel.fromJson(Map<String, dynamic> json)
      : id = json['id'] ?? 0,
        version = json['version'] ?? ConstantsUtils.version,
        name = json['name'] ?? '临时调色板',
        lastUseColor = json['lastUseColor'] ?? 0,
        data =
            (json['data'] as List?)
                ?.map((e) => ColorItem.fromJson(e))
                .cast<ColorItem>()
                .toList() ??
                [];

  Map<String, dynamic> toJson() =>
      {
        'id': id,
        'version': version,
        'name': name,
        'lastUseColor': lastUseColor,
        'data': data.map((e) => e.toJson()).toList(),
      };
}

/// 颜色项
class ColorItem {
  /// 名称
  String name;

  /// 描述
  String description;

  /// 是否可见
  bool view;

  /// 颜色
  Color color;

  ColorItem({
    required this.name,
    required this.description,
    required this.view,
    required this.color,
  });

  factory ColorItem.fromJson(Map<String, dynamic> json) {
    return ColorItem(
      name: json['name']??'',
      description: json['description'] ?? '',
      view: json['view'] ?? true,
      color: CommonUtils.hex2Color(json['color']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'description': description,
      'view': view,
      'color': CommonUtils.color2Hex(color),
    };
  }
}
